/**
 * nullFunction:定一个空的函数
 * eeesysCallSure:用于确定时回调执行的程序的对象,可以不需要
 * todo:  alert可以有两个参数（内容和标题），也可以是一个参数，默认标题为通知
 */
var nullFunction = function() {}
// var eeesysCallSure = nullFunction;

// window.confirm = function(data)
// {
// 	var sureContent = data.sureContent || "确认";
// 	var cancleContent = data.cancleContent || "取消";
// 	var success = data.success || nullFunction;
// 	var cancel = data.cancel || nullFunction;
// 	var txt = data.title || "";
// 	var cancelTime = 0;
// 	var successTime = 0;
// 	//如果没有title参数，默认为通知
// 	if(!arguments[1]) eeesysCallSure = nullFunction;
// 	//创建alert对话框
// 	var alertFram = document.createElement("DIV");
// 	alertFram.id="eeesysAlertBody";
// 	alertFram.className = 'eeesysAlertBodys';
// 	strHtml = "<div id='eeesysAlert'>";
// 	strHtml += "<div id='eeesysContent'><p>" +  txt + "</p></div>";
// 	strHtml += "<div id='eeesysButton'>";
// 	strHtml += "<p id='eeesysSure' onclick='eeesysSure(this)'>" + sureContent + "</p>";
// 	strHtml += "<p id='eeesysCancel' onclick='eeesysCancel(this)'> " + cancleContent + "</p>";
// 	strHtml += "</div>";
// 	strHtml += "</div>";
//
// 	alertFram.innerHTML = strHtml;
// 	document.body.appendChild(alertFram);
//
// 	this.onkeydown = function(obj) {
// 		var e = event || window.event || arguments.callee.caller.arguments[0];
// 		if(e && e.keyCode == 27){ // 按 Esc
// 		 //要做的事情
// 		}
// 		if(e && e.keyCode == 113){ // 按 F2
// 		  //要做的事情
// 		}
// 		if(e && e.keyCode == 13){ // enter 键
// 			var sureObj = document.getElementById("eeesysSure");
// 			sureObj && eeesysSure(sureObj);
// 			event.preventDefault();
// 		}
// 	};
// 	//确认按钮
// 	this.eeesysSure = function(obj, tag){
// 		if (successTime == 1) {
// 			return;
// 		}
// 		successTime++;
// 		var alertBody = alertFram;
// 		success();
// 		alertBody && document.body.removeChild(alertBody);
// 	}
// 	//取消按钮
// 	this.eeesysCancel = function(obj, tag){
// 		if (cancelTime == 1) {
// 			return;
// 		}
// 		if (tag == 2) {
// 			obj = obj.parentNode;
// 		}
// 		cancelTime++;
// 		var alertBody = alertFram;
// 		var alertObj = alertBody.querySelector('#eeesysAlert');
//
// 		 //判断是否有jquery
// 		alertObj.classList.remove("bounceIn");
// 		alertObj.classList.add("bounceOut");
// 		cancel();
// 		alertBody && document.body.removeChild(alertBody);
// 	}
// 	alertFram.focus();
// 	document.body.onselectstart = function(){return false;};
// }
var confirmSingle = function(data) {
	var sureContent = data.sureContent || "确认";
	var cancleContent = data.cancleContent || "取消";
	var success = data.success || nullFunction;
	var txt = data.title || "";
	var successTime = 0;
	//如果没有title参数，默认为通知
	if(!arguments[1]) eeesysCallSure = nullFunction;
	//创建alert对话框
	var alertFram = document.createElement("DIV");
	alertFram.id="eeesysAlertBody";
	alertFram.className = 'eeesysAlertBodys';

	strHtml = "<div id='eeesysAlert'>";
	strHtml += "<div id='eeesysContent'><p>" +  txt + "</p></div>";
	strHtml += "<div id='eeesysButton'>";
	strHtml += "<p id='eeesysSure' onclick='eeesysSure(this)'>" + sureContent + "</p>";
	strHtml += "</div>";
	strHtml += "</div>";

	alertFram.innerHTML = strHtml;
	document.body.appendChild(alertFram);

	this.onkeydown = function(obj) {
		var e = event || window.event || arguments.callee.caller.arguments[0];
		if(e && e.keyCode == 27){ // 按 Esc
		 //要做的事情
		}
		if(e && e.keyCode == 113){ // 按 F2
		  //要做的事情
		}
		if(e && e.keyCode == 13){ // enter 键
			var sureObj = document.getElementById("eeesysSure");
			sureObj && eeesysSure(sureObj);
			event.preventDefault();
		}
	};
	//确认按钮
	this.eeesysSure = function(obj){
		if (successTime == 1) {
			return;
		}
		successTime++;
		var alertBody = alertFram;
		var alertObj = alertBody.querySelector('#eeesysAlert');
		alertObj.classList.remove("bounceIn");
		alertObj.classList.add("bounceOut");
		success();
		alertBody && document.body.removeChild(alertBody);
	}
	alertFram.focus();
	document.body.onselectstart = function(){return false;};
}
//loading加载
var eeesysLoading = function (data) {
	var data = data || {};
    var content = data.content || "请等待";
		var bodyFram = document.createElement( "DIV" );
				bodyFram.id = "eeesysLoading";
    var loadingFram = document.createElement( "DIV" );
        loadingFram.id = "eeesysLoadingBody";
    var strHtml = /* '<div id="eeesys-loading-orange" class="eeesys-loading-box"></div>' +
									'<div id="eeesys-loading-green" class="eeesys-loading-box"></div>' +
									'<div id="eeesys-loading-yellow" class="eeesys-loading-box"></div>'; */
				"<div class='sk-three-bounce'>"+
					"<div class='sk-child sk-bounce1'></div>"+
					"<div class='sk-child sk-bounce2'></div>"+
					"<div class='sk-child sk-bounce3'></div>"+
				"</div>";
    loadingFram.innerHTML = strHtml;
		bodyFram.appendChild(loadingFram);
	var body = document.body;
	if (data.el) {
		body = document.querySelector(data.el);
	}
    body.appendChild( bodyFram );
}
//loading停止
var eeesysLoadingCancel = function (data) {
	var data = data || {};
  var loadingFram = document.getElementById("eeesysLoading");
	var body = document.body;
	if (data.el) {
		body = document.querySelector(data.el);
	}
	loadingFram && loadingFram.parentNode.removeChild( loadingFram )
}
var eeesysPlugin = eeesysPlugin || {};
eeesysPlugin.menu = function(data) {
	var body = document.querySelector("body");
	this.eeesysPluginCancel = function() {
		body.removeChild(rootDom);
		event.stopPropagation();
	}
	var menus = data.menus || [];
	var rootDom = document.createElement("div");
	rootDom.className = "eeesysPlugin-menu-big";
	var parentDom = document.createElement("div");
	rootDom.appendChild(parentDom);
	parentDom.className = "eeesysPlugin-menu-half animated fadeInUpBig";
	rootDom.addEventListener("click", this.eeesysPluginCancel);
	if (data.tip) {
		var tipDom = document.createElement("div");
		parentDom.appendChild(tipDom);
		tipDom.className = "eeesysPlugin-menu-tip";
		tipDom.innerHTML = '<p>' + data.tip + '</p>';
	}

	menus.forEach(function(item) {
		var dom = document.createElement("div");
		dom.className = "eeesysPlugin-menu-item";
		dom.innerHTML = "<p>" + item.txt + "</p>";
		dom.addEventListener("click", this.eeesysPluginCancel);
		dom.addEventListener("click", item.func);
		parentDom.appendChild(dom);
	}.bind(this));
	var cancelDom = document.createElement("div");
	parentDom.appendChild(cancelDom);
	cancelDom.className = "eeesysPlugin-menu-last";
	cancelDom.addEventListener("click", this.eeesysPluginCancel);
	cancelDom.innerHTML = '<p>取消</p>';
	body.appendChild(rootDom);
}
eeesysPlugin.ok = function(data) {
	var body = document.querySelector("body");
	var rootDom = document.createElement("div");
	rootDom.className = "eeesysplugin-finished-box";
	var html = '<div class="eeesysplugin-finished-yes">' +
					'<img src="/style/eeesysPlugin/v2/ok.png" alt="已完成" />' +
					'<p>' + (data.txt || "操作成功") + '</p>' +
				'</div>';
	rootDom.innerHTML = html;
	body.appendChild(rootDom);
	setTimeout(function() {
		body.removeChild(rootDom);
	}, 1000);
}
